#include <iostream>
#include "actions.h"

using namespace std;

/*----------------------------------------------------------------------------*/
//Constructor
actions::actions(int rows, int cols)
{
  //Array2d here instead?
  board = new int*[rows];
  for (int i = 0; i < r; i++)
      ptr[i] = new int[col];

}
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*** Check for a winner in a given row and return EMPTY(0?) if there is no
     winner, or the color of winning side RED(1?) or BLUE(2?)***/


int actions::checkWinnerRow() 
{
//Needs tweaking for the actual game board once we get the code for that 
    for (int i = 0; i < COLUMNS; i++) 
    {
        if (board[row][i] != EMPTY) &&
           (board[row][i] == board[row][i+1]) &&
           (board[row][i] == board[row][i+2]) &&
           (board[row][i] == board[row][i+3])) 
           {
                          return board[row][i];
           }

    }

        // return EMPTY if no winner
        return EMPTY;
};
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*** Check for a winner in a given column and return EMPTY(0?) if there is no
     winner, or the color of winning side RED(1?) or BLUE(2?)***/


int actions::checkWinnerCol() 
{
//Needs tweaking for the actual game board once we get the code for that 
    for (int i = 0; i < ROWS; i++) 
    {
        if (board[i][col] != EMPTY) &&
           (board[i][col] == board[i+1][col]) &&
           (board[i][col] == board[i+2][col]) &&
           (board[i][col] == board[i+3][col])) 
           {
                          return board[i][col];
           }

    }

        // return EMPTY if no winner
        return EMPTY;
}
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*** Check for a winner in a given diagonal and return EMPTY(0?) if there is no
     winner, or the color of winning side RED(1?) or BLUE(2?)***/

//Needs tweaking when we get the game board code     
int actions::checkWinnerDiag() 
{
        int x = row, y = col;
        
        // move to top-left
        while ((x > 0) && (y > 0)) 
        {
            x--;
            y--;
        }
        
        // check down-right diagonals
        while ((x < ROWS) && (y < COLUMNS)) 
        {
            if ((board[x][y] != EMPTY) &&
                (board[x][y] == board[x+1][y+1]) &&
                (board[x+1][y+1] == board[x+2][y+2]) &&
                (board[x+2][y+2] == board[x+3][y+3])) 
                {
                                 return board[row][col];
                }
            x++;
            y++;
        }
        
        // reset for next test
        x = row;
        y = col;
        
        // move to top-right
        while ((x < ROWS) && (y > 0)) 
        {
            x++;
            y--;
        }
        
        //check down-left diagonals
        while ((x > 0) && (y < COLUMNS)) 
        {
            if ((board[x][y] != EMPTY) &&
                (board[x][y] == board[x-1][y+1]) &&
                (board[x-1][y+1] == board[x-2][y+2]) &&
                (board[x-2][y+2] == board[x-3][y+3])) 
                {
                                 return board[row][col];
                }
                
            x--;
            y++;
        }
        
        // return EMPTY if there is no winner
        return EMPTY;
    }
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*** Check for a tie in and return true for a tie or false to keep playing.***/
bool actions::checkTie()
{
  int check = 0;
  for (int i = 0; i < cols; i++)
      if (board[0][i] != EMPTY) //Needs to be board[firstGameRow][i] instead, see why below
         check++;

  if (check == cols)
     return true;
  /*This needs to be something like
  if (check == numGameCols) instead,
  but we need the board's format before we can do that */

  else
      return false;

}
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*This function gets the column number the user wishes to drop the token into in addition to making sure the response is valid. 
if an invalid response is given, the user must re-enter until a valid anser is given*/
int actions::getDropCol(int rows, int cols, int** []board/*not sure if that's how you pass it*/){
	int dropCol;

	//data will need to be formatted once we get this thing running
	cout << "Enter the column number you wish to drop you token into: ";
	cin >> dropCol;

	//This while loop check for 3 things: 1) that the column number is not less than 1; 2)that the column number is not greater than the maximum number of columns given;  3)that the column number given is not already full of tokens
	while(dropCol < 1 || dropCol > cols || board[rows][dropCol] != 0 /*This assumes the array will be made of numbers and zero is a neutral space - must be edited otherwise */){
		cout << endl << "/aInvalid Entry - Please enter a valid column number: ";
		cin >> dropCol;
		}//end of while loop that validates data entry
		
	return dropCol;
}
/*----------------------------------------------------------------------------*/
/*This function inserts the player number into the correct part of the array so that his or her token will be saved there.*/
void actions::dropToken(int rows/*max # of rows*/, int drop/*column in which token is dropped*/, int player/*player number - used to change the array*/, int** []board/*again - I need to make sure this is right*/){
	int i = rows - 1;//we can set it this low becuase we know that the first row will be empty from the previous function
	
	//this loop checks to see if a token is beneath the current spot; it walks down the board until it finds another token or the bottom.
	while(board[i-1][drop] == 0 && i !=0){
		i--;
	}
	
	board[i][drop] = player;//sets the found position to the player's number
}
/*----------------------------------------------------------------------------*/
/*this function just switches the player - call it at the end of a turn to change it*/
void actions::switchPlayer(int& player){
	if(player == 1)
		player = 2;
	else
		player = 1;
}


/*----------------------------------------------------------------------------*/
actions::~actions()
{
}
/*----------------------------------------------------------------------------*/
